<template>
  <div class="question-randoms">
    <el-card class="app">
      <!-- 关键字搜索组件 -->
      <QuestionsSearch>
        <template #left>
          <span>关键字</span>
          <el-input v-model="queryData.keyword" style="width:250px;margin-left:10px" clearable placeholder="请输入编号搜索"></el-input>
        </template>
        <template #right>
          <el-button @click="clearInput">清除</el-button>
          <el-button type="primary" @click="searchFn">搜索</el-button>
        </template>
      </QuestionsSearch>
      <!-- 公司提示信息 -->
      <div class="tip" body-style="color:gray" style="margin-top: 15px;">
        <i class="el-icon-info" style="margin-right: 10px;" />
        <span>数据一共 {{pageTotal}} 条</span>
      </div>

      <div style="margin-top: 15px;">
        <el-table :data="tableData" align="center">
          <el-table-column prop="id" label="编号" width="270">
          </el-table-column>
          <el-table-column prop="questionType" label="题型" width="160">
            <template v-slot="scope">
              {{formatType(scope.row.questionType)}}
            </template>
          </el-table-column>

          <el-table-column label="题目编号" width="270">
            <template v-slot="scope">
              <QuestionsLink :linkData="scope.row" @open-channel="openChannel">
              </QuestionsLink>
            </template>
          </el-table-column>

          <el-table-column prop="addTime" label="录入时间" width="270">
          </el-table-column>

          <el-table-column prop="totalSeconds" label="答题时间(S)" width="160">
          </el-table-column>

          <el-table-column prop="accuracyRate" label="正确率(%)" width="160">
          </el-table-column>

          <el-table-column prop="userName" label="录入人" width="160">
          </el-table-column>
          <el-table-column label="操作" width="160">
            <template v-slot="{row}">
              <el-button type="danger" icon="el-icon-delete" circle plain @click="del(row.id)"></el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页功能组件 -->
        <div style="height: 60px; margin-top: 30px">
          <el-pagination background :page-size="20" layout=" ->, prev, pager, next,sizes,jumper" @current-change="changePage" @size-change="changeSize" :page-sizes="[20, 30, 40]" :total="pageTotal">
          </el-pagination>
        </div>
      </div>

      <!-- 弹窗组件 -->
      <el-dialog title="题目预览" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
        <div class="channel">
          <el-row>
            <el-col :span="8">【题型】：{{question_types}}</el-col>
            <el-col :span="8">【编号】：{{subjectPreview.id}}</el-col>
            <el-col :span="8">【标签】：{{subjectPreview.tags}}</el-col>
          </el-row>
          <el-row style="margin-top:15px">
            <el-col :span="8">【学科】： {{subjectPreview.subjectName}}</el-col>
            <el-col :span="8">【目录】： {{subjectPreview.directoryName}}</el-col>
            <el-col :span="8">【方向】： {{subjectPreview.direction}}</el-col>
          </el-row>
          <el-divider></el-divider>
          <el-row>【题干】:{{question_types}}</el-row>
          <span style="margin:10px 0" v-html="subjectPreview.question"></span>
          <el-radio-group style="margin-top:20px" v-model="radio" v-if="subjectPreview.questionType === '1'">
            <el-radio :label="item.id" v-for="item in subjectPreview.options" :key="item.id">{{item.code}}</el-radio>
          </el-radio-group>
          <el-checkbox-group style="margin:20px 0" v-else-if="subjectPreview.questionType === '2'" v-model="checkList">
            <el-checkbox :label="item.id" v-for="item in subjectPreview.options" :key="item.id">{{item.code}}</el-checkbox>
          </el-checkbox-group>
          <el-divider></el-divider>
          <el-row>【参考答案】：<el-button type="danger" @click="isShow=!isShow">视频答案预览</el-button>
            <video style="background-color:lightgray;margin-top:10px" :src="subjectPreview.videoURL" v-show="isShow"></video>
          </el-row>
          <el-divider></el-divider>
          <el-row>【答案解析】：<span v-html="subjectPreview.answer"></span></el-row>
          <el-divider></el-divider>
          <span>【题目备注】： {{subjectPreview.remarks}}</span>
          <span slot="footer" class="dialog-footer">
          </span>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
          </div>
        </div>
      </el-dialog>
    </el-card>
  </div>

</template>

<script>
import QuestionsSearch from './components/questions-search.vue'
import QuestionsLink from './components/questions-link.vue'
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'
export default {
  components: { QuestionsSearch, QuestionsLink },
  data () {
    return {
      // 组题列表
      tableData: [{
        id: 0, // 编号
        questionType: '', // 题型
        questionsIDs: [], // 题目编号
        addTime: '', // 录入时间
        totalSeconds: '', // 答题时间
        accuracyRate: '', // 正确率
        userName: '' // 录入人
      }],

      // 题目预览弹窗数据
      subjectPreview: {},

      // 组题列表总数
      pageTotal: 0,

      // 分页
      queryData: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },

      dialogVisible: false, // 控制弹窗
      radio: null, // 控制单选
      checkList: [], // 控制复选
      isShow: false // 视频显示和隐藏
    }
  },
  computed: {
    // 弹窗显示名称
    question_types () {
      if (this.subjectPreview.questionType === '1') {
        return '单选题'
      } else if (this.subjectPreview.questionType === '2') {
        return '多选题'
      } return '简答题'
    }
  },
  created () {
    this.getQuestionsList()
  },
  methods: {
    formatType (type) {
      if (type === '1') {
        return '单选'
      } else if (type === '2') {
        return '多选'
      } return '简答题'
    },
    // 获取列表数据
    async getQuestionsList () {
      try {
        const ret = await randoms(this.queryData)
        if (ret.status !== 200) return this.$message.error('获取题组列表失败')
        this.tableData = ret.data.items
        this.pageTotal = ret.data.counts
      } catch {
        this.$message.error('获取题组列表失败')
      }
    },

    // 删除功能
    async del (id) {
      const ret = await this.$confirm('确认要删除吗？, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 取消
      if (ret === 'cancel') return

      const delRet = await removeRandoms({ id })
      if (delRet.status === 200) {
        this.$message.success('删除成功')
        this.getQuestionsList()
      }
    },

    // 清除input框输入的内容
    clearInput () {
      this.queryData.keyword = ''
      this.getQuestionsList()
    },

    // 搜索功能
    searchFn () {
      this.getQuestionsList()
    },

    // 切换每页条数
    changeSize (size) {
      this.queryData.page = 1
      this.queryData.pagesize = size
      this.getQuestionsList()
    },

    // 控制页的变化
    changePage (page) {
      this.queryData.page = page
      this.getQuestionsList()
    },

    // 下拉框控制页码变化
    handleSizeChange (val, page) {
      this.queryData.page = page
      this.queryData.pagesize = val
      this.getQuestionsList()
    },

    // 输入页码回车控制页码变化
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
    },

    // 打开弹窗->子派发父监听 (根据编码显示对应内容（基础题库详情）)
    async openChannel (Qid) {
      this.tableData.id = Qid
      this.dialogVisible = true
      const ret = await detail({ id: Qid })
      this.subjectPreview = ret.data
      this.subjectPreview.options.forEach(item => {
        if (item.isRight === 1) {
          console.log(item.id)
          this.radio = item.id
        } else if (item.isRight === 0) {
          this.checkList.push(item.id)
        }
      })
      console.log(this.subjectPreview)
    },
    // 关闭弹窗
    async handleClose (done) {
      this.$confirm('确认关闭吗？')
        .then(_ => {
          done()
        })
        .catch(_ => { })
    }
  }

}
</script>

<style scoped lang='scss'>
.link {
  color: blue;
}
.tip {
  border-radius: 5px;
  padding-left: 15px;
  height: 30px;
  line-height: 30px;
  background-color: rgb(242, 240, 240);
}
.dialog-footer {
  display: flex;
  justify-content: right;
  margin-top: 15px;
}
</style>
